package cn.exrick.xboot.modules.content.serviceimpl;

import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.utils.SecurityUtil;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.modules.base.entity.RolePermission;
import cn.exrick.xboot.modules.base.entity.User;
import cn.exrick.xboot.modules.content.dao.mapper.HontUserPermissionMapper;
import cn.exrick.xboot.modules.content.entity.HontPermission;
import cn.exrick.xboot.modules.content.entity.HontUser;
import cn.exrick.xboot.modules.content.entity.HontUserPermission;

import cn.exrick.xboot.modules.content.service.IHontUserPermissionService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zy
 * @since 2022-10-26
 */
@Service
public class HontUserPermissionServiceImpl extends ServiceImpl<HontUserPermissionMapper, HontUserPermission> implements IHontUserPermissionService {
   @Autowired
    HontUserPermissionMapper hontUserPermissionMapper;
    @Autowired
    private SecurityUtil securityUtil;
    public HontUserPermissionServiceImpl() {
        super();
    }

    @Override
    public List<HontUserPermission> selectAllList() {
        QueryWrapper<HontUserPermission> query=new QueryWrapper();
        List<HontUserPermission> hontUserPermissions = hontUserPermissionMapper.selectList(query);
        return hontUserPermissions;
    }

    @Override
    public List<HontUserPermission> findByPermissionId(String id) {
        QueryWrapper<HontUserPermission> query=new QueryWrapper();
        query.lambda().eq(HontUserPermission::getHontPermissionId, id);
        List<HontUserPermission> hontUserPermissions = hontUserPermissionMapper.selectList(query);
        return hontUserPermissions;
    }

    @Override
    public void deleteByUserId(String userId) {
        QueryWrapper<HontUserPermission> query=new QueryWrapper();
        query.lambda().eq(HontUserPermission::getHontUserId, userId);
        hontUserPermissionMapper.delete(query);
    }

    @Override
    public List<HontUserPermission> findByHontUserId(String id) {
        QueryWrapper<HontUserPermission> query=new QueryWrapper();
        query.lambda().eq(HontUserPermission::getHontUserId, id);
       return hontUserPermissionMapper.selectList(query);
    }

    @Override
    public Result<Object> editUserPerm(String userId, String permIds) {
        HontUser currUser = securityUtil.getCurrUser();
        String[] array = permIds.split(",");
        //删除其关联权限
         deleteByUserId(userId);
        //分配新权限
        for(String permId : array){
            HontUserPermission userPermission = new HontUserPermission();
            userPermission.setHontUserId(userId);
            userPermission.setHontPermissionId(permId);
            userPermission.setCreateTime(LocalDateTime.now());
            userPermission.setUpdateTime(LocalDateTime.now());
            userPermission.setCreateBy(currUser.getNickName());
            userPermission.setUpdateBy(currUser.getNickName());
            this.save(userPermission);
        }
        List<HontUserPermission> byRoleId = this.findByHontUserId(userId);
        return ResultUtil.data(byRoleId);
    }

    @Override
    public List<HontPermission> findMenuByUserId(String userId) {
        return hontUserPermissionMapper.findMenuByUserId(userId);
    }
}
